package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {


    @Autowired
    private  SetmealMapper setmealMapper;
    @Autowired
    private  DishMapper dishMapper;
    @Autowired
    private  ShoppingCartMapper shoppingCartMapper;


    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {


        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //此时购物车中属性是三个基础属性都有了，直接进行补充就行了

        //先判断该用户的购物车的当前商品是不是已经存在了
        ShoppingCart scart = shoppingCartMapper.selectBy(shoppingCart);

        //scart获得是对象指向空null，无法完成对象的封装
        //如果查询到了 scart就有信息了，不行在新建一个对象完成封装，太多余了，只额吉使用查询后的对象就行

        if (scart == null) {
            //新建购物车
            //首先判断是不是菜品还是套餐
            if(shoppingCart.getSetmealId() != null) {

                //证明是套餐
                //根据套餐的id查询套餐
                Setmeal setmeal = setmealMapper.selectSetmealById(shoppingCart.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                //第一次添加设为1
                shoppingCart.setNumber(1);
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setCreateTime(LocalDateTime.now());

            }else{
                //是菜品
                //直接在shoppingCart中封装未知属性
                Dish dish = dishMapper.selectById(shoppingCart.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                //第一次添加设为1
                shoppingCart.setNumber(1);
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setCreateTime(LocalDateTime.now());

            }
            shoppingCartMapper.insert(shoppingCart);

        } else {
            //修改购物车
            scart.setNumber(scart.getNumber() + 1);
            //scat封装完成，提交购物车
            shoppingCartMapper.update(scart);
        }





    }

    @Override
    public List<ShoppingCart> list() {
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(BaseContext.getCurrentId());
        return shoppingCartList;
    }

    /**
     * 根据用户的id清空购物车
     */

    @Override
    public void clean() {
        shoppingCartMapper.deleteAllById(BaseContext.getCurrentId());

    }

    /**
     *
     * @param shoppingCartDTO
     */
    @Override
    public void deleteBy(ShoppingCartDTO shoppingCartDTO) {
        //首先进行判断 是套餐还是菜品
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());


            //补充属性
             shoppingCart = shoppingCartMapper.selectBy(shoppingCart);
             //修改数量
            //判断是不是为0，如果是0的话，直接删除这一条记录
            if(shoppingCart.getNumber() == 1){
                shoppingCartMapper.deleteBy(shoppingCart);
            }else {
                shoppingCart.setNumber(shoppingCart.getNumber() - 1);
                //更新购物车
                shoppingCartMapper.update(shoppingCart);
            }


    }
}